package com.xcity.game.partner;

import java.text.MessageFormat;

import com.xcity.db.entity.Mine;
import com.xcity.db.entity.Player;
import com.xcity.db.entity.User;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.account.AccountService;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2Part;
import com.xcity.game.common.MessageConst;
import com.xcity.game.event.GameEvents;
import com.xcity.game.player.robot.RobotUtils;
import com.xcity.util.PlatformApi;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.service.Service;
import naga.x.service.ServiceException;

/**
 * 玩吧渠道相关服务<br/>
 * @see AccountChannel#QQ_WANBA
 * @see AccountChannel#QQ
 * @author yang.li
 *
 */
public class PartnerQQService implements Service, EventListener {

	@Override
	public void startup() throws ServiceException {
		App.getApp().getEventManager().register(this);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}

	@Override
	public int[] getEventTypes() {
		return new int[] {
				GameEvents.EVENT_ADVENTURE_STEAL,
				GameEvents.EVENT_BUILDING_CAPTURE,
				GameEvents.EVENT_ADVENTURE_ATTACK,
				GameEvents.EVENT_MINE_ATTACK_SUCC
		};
	}

	@Override
	public void handleEvent(Event event) {
		switch (event.getType()) {
			case GameEvents.EVENT_ADVENTURE_STEAL:
				fireSteal(event.getParameter(0), event.getParameter(3), event.getParameter(4));
				break;
			case GameEvents.EVENT_BUILDING_CAPTURE:
				fireCapture(event.getParameter(0), event.getParameter(1));
				break;
			case GameEvents.EVENT_ADVENTURE_ATTACK:
				fireAttack(event.getParameter(0), event.getParameter(1), event.getParameter(2), event.getParameter(3), event.getParameter(4));
				break;
			case GameEvents.EVENT_MINE_ATTACK_SUCC:
				fireMineAttacked(event.getParameter(0), event.getParameter(1), event.getParameter(2));
				break;
		}
	}
	
	private void fireSteal(Player source, Player dest, long gold) {
		if (validate(source) && validate(dest)) {
			String name = source.getName().equals(source.getAccount().getNickname()) ? "" : source.getName();
			PlatformApi.sendQQMsg(source, dest,
					MessageFormat.format(MessageConst.ADVENTURE_BE_STOLEN_BY_QQ_FRIEND,
							name, gold));
		}
	}
	
	private void fireCapture(Player player, BuildV2 building) {
		Player dest = building.getVm().getOwner();
		if (validate(player) && validate(dest)) {
			String name = player.getName().equals(player.getAccount().getNickname()) ? "" : player.getName();
			PlatformApi.sendQQMsg(player, dest,
					MessageFormat.format(MessageConst.CAPTURE_BY_QQ_FRIEND,
							name, building.getTemplate().getName()));
		}
	}
	
	private void fireAttack(Player source, Player dest, BuildV2 building, BuildV2Part buildingPart, boolean isHit) {
		if (isHit && validate(source) && validate(dest)) {
			String name = source.getName().equals(source.getAccount().getNickname()) ? "" : source.getName();
			PlatformApi.sendQQMsg(source, dest,
					MessageFormat.format(MessageConst.ADVENTURE_BE_ATTACKED_BY_QQ_FRIEND,
							name, building.getTemplate().getName(), buildingPart.getTemplate().name));
		}
	}
	
	private void fireMineAttacked(Player source, Player dest, Mine mine) {
		if (validate(source) && validate(dest)) {
			String name = source.getName().equals(source.getAccount().getNickname()) ? "" : source.getName();
			PlatformApi.sendQQMsg(source, dest,
					MessageFormat.format(MessageConst.MINE_ATTACKED_BY_QQ_FRIEND, name));
		}
	}
	
	private static boolean validate(Player player) {
		if (RobotUtils.isRobot(player)) {
			return false;
		}
		User acc = player.getAccount();
		if (acc == null) {
			acc = App.getApp().getServiceManager().get(AccountService.class).findById(player.getUserId());
			player.setAccount(acc);
		}
		return acc != null && (acc.getChannel() == AccountChannel.QQ || acc.getChannel() == AccountChannel.QQ_WANBA);
	}

}
